package cn.zf233.jwtshiro.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * Created by zf233 on 2021/7/12
 * 统一返回格式
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R<T> implements Serializable {

    private static final long serialVersionUID = -8020751417469577331L;

    private final Integer status;
    private String msg;
    private T data;

    private R(int status) {
        this.status = status;
    }

    private R(int status, T data) {
        this.status = status;
        this.data = data;
    }

    private R(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    private R(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    @JsonIgnore
    //使之不在json序列化结果当中
    public boolean isSuccess() {
        return this.status == ResponseCodeENUM.SUCCESS.getCode();
    }

    public int getStatus() {
        return status;
    }

    public T getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public static <T> R<T> createBySuccess() {
        return new R<>(ResponseCodeENUM.SUCCESS.getCode());
    }

    public static <T> R<T> createBySuccessMessage(String msg) {
        return new R<>(ResponseCodeENUM.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> createBySuccess(T data) {
        return new R<>(ResponseCodeENUM.SUCCESS.getCode(), data);
    }

    public static <T> R<T> createBySuccess(String msg, T data) {
        return new R<>(ResponseCodeENUM.SUCCESS.getCode(), msg, data);
    }

    public static <T> R<T> createByError() {
        return new R<>(ResponseCodeENUM.ERROR.getCode(), ResponseCodeENUM.ERROR.getDesc());
    }

    public static <T> R<T> createByError(String errorMessage, T data) {
        return new R<>(ResponseCodeENUM.ERROR.getCode(), errorMessage, data);
    }

    public static <T> R<T> createByErrorMessage(String errorMessage) {
        return new R<>(ResponseCodeENUM.ERROR.getCode(), errorMessage);
    }

    public static <T> R<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new R<>(errorCode, errorMessage);
    }

    public static <T> R<T> createByErrorCodeMessage(int errorCode, String errorMessage, T data) {
        return new R<>(errorCode, errorMessage, data);
    }
}
